import api from '@/store/api';
import i18n from '@/i18n';
const { t } = i18n.global as any;

export const useServerLedStore = defineStore('ServerLedStore', () => {
  const indicatorLedActiveState = ref(false);
  const setIndicatorLedActiveState = (state: any) => {
    indicatorLedActiveState.value = state;
  };
  const getIndicatorLedActiveState = async () => {
    return await api
      .get('/redfish/v1/Systems/system')
      .then((response) => {
        setIndicatorLedActiveState(response.data.LocationIndicatorActive);
      })
      .catch((error) => console.log(error));
  };
  const saveIndicatorLedActiveState = async (payload: any) => {
    setIndicatorLedActiveState(payload);
    return await api
      .patch('/redfish/v1/Systems/system', {
        LocationIndicatorActive: payload,
      })
      .catch((error) => {
        console.log(error);
        setIndicatorLedActiveState(!payload);
        if (payload) {
          throw new Error(
            t('pageInventory.toast.errorEnableIdentifyLed') as string,
          );
        } else {
          throw new Error(
            t('pageInventory.toast.errorDisableIdentifyLed') as string,
          );
        }
      });
  };

  return {
    indicatorLedActiveState,
    setIndicatorLedActiveState,
    getIndicatorLedActiveState,
    saveIndicatorLedActiveState,
  };
});
